﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using System.Data.Linq;
namespace Exact_Extension
{
    public partial class Discount : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        int LastRowHandleDiscount = 0;
        public Discount(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
         
        }




        private void LoadData()
        {
            var queryDisMaster = from DisMaster in DS.ISHCMC_DiscountMasters
                                 select DisMaster;
            this.iSHCMCDiscountMasterBindingSource.DataSource = queryDisMaster;

            this.lookUpEdit_DiscountType.ItemIndex = 0;
            
            var queryStudent = from Student in DS.ISHCMC_V_StudentListWithDebtors
                               select Student;
            this.iSHCMCVStudentListWithDebtorBindingSource.DataSource = queryStudent;

            this.gridView_Student.SelectRow(0);
          
        }
        private void LoadAllDiscount()
        {
            var queryDis = from Dis in DS.ISHCMC_V_DiscountLists
                           select Dis;
            this.iSHCMCVDiscountListBindingSource.DataSource = queryDis;
        }
        private void LoadDiscountPerStudent(string  studentID)
        {
            var queryDis = from Dis in DS.ISHCMC_V_DiscountLists
                           where Dis.StudentID == studentID
                           select Dis;
            this.iSHCMCVDiscountListBindingSource.DataSource = queryDis;

        }
      
        private bool CheckExist(string StudentID,string DiscountType)
        {
            var query = from Dis in DS.ISHCMC_Discounts
                        where Dis.StudentID == StudentID && Dis.DiscountType == DiscountType
                        && Dis.Status ==0
                        select Dis;
            if (query.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            this.LoadData();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            string discounttype ="";
        
            int flag = 0;
            if (this.lookUpEdit_DiscountType.EditValue != null)
            {
                discounttype = this.lookUpEdit_DiscountType.EditValue.ToString();
                flag = 1;
            }
            else
            {
                flag = 0;
                MessageBox.Show("Please choose discount type !!");


            }
          
            if (flag == 1)
            {
                if (this.gridView_Student.GetSelectedRows().Count() > 0)
                {
                    ISHCMC_V_StudentListWithDebtor student = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(this.gridView_Student.GetSelectedRows()[0]);
                    if (!CheckExist(student.StudentID, discounttype))
                    {
                        ISHCMC_Discount dis = new ISHCMC_Discount
                        {
                            Debcode = student.DebtorCode,
                            StudentID = student.StudentID,
                            DiscountType = discounttype,
                            syscreated = DateTime.Now,
                            sysmodified = DateTime.Now,
                            Status = 0
                        };
                        DS.ISHCMC_Discounts.InsertOnSubmit(dis);
                        DS.SubmitChanges();
                    }
                    else
                    {
                        MessageBox.Show("Discount type already assigned to this student , please try to check again !!");
                    }
                    this.LoadDiscountPerStudent(student.StudentID);
                }
                else
                {
                    MessageBox.Show("Please Choose student to assigned discount!!!");
                }
            }
           
        }

       

        private void gridView_Discount_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.popupMenu1.ShowPopup(Control.MousePosition);
                LastRowHandleDiscount = e.RowHandle;
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                ISHCMC_V_DiscountList itemVDiscount = (ISHCMC_V_DiscountList)this.gridView_Discount.GetRow(LastRowHandleDiscount);
                ISHCMC_Discount itemDis = DS.ISHCMC_Discounts.Single(c => c.ID == itemVDiscount.ID);
                itemDis.Status = 1;
                DS.SubmitChanges();
                this.LoadDiscountPerStudent(itemVDiscount.StudentID);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void labelControl1_Click(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

        }

        private void gridView_Student_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            if (this.gridView_Student.GetSelectedRows().Count() > 0)
            {
                ISHCMC_V_StudentListWithDebtor student = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(this.gridView_Student.GetSelectedRows()[0]);
                LoadDiscountPerStudent(student.StudentID);
            }


        }

        private void chk_SeeAllDiscount_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_SeeAllDiscount.Checked)
            {
                this.LoadAllDiscount();
            }
            else
            {
                if (this.gridView_Student.RowCount > 0)
                {
                    this.gridView_Student.SelectRow(0);
                }
            }
        }

        private void chk_SelectAllST_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_SelectAllST.Checked)
            {
                this.gridView_Student.SelectAll();
            }
            else
            {
                if (this.gridView_Student.RowCount > 0)
                {
                    this.gridView_Student.ClearSelection();
                    this.gridView_Student.SelectRow(0);
                }
            }
        }

        private void Discount_Shown(object sender, EventArgs e)
        {
            this.LoadData();
        }

        private void btnAttachment_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_V_DiscountList DisDetail = (ISHCMC_V_DiscountList)this.gridView_Discount.GetRow(LastRowHandleDiscount);

            DiscountAttach DisAttach = new DiscountAttach(DS, DisDetail);
            DisAttach.Text = DisAttach.Text + " Discount Type :" + DisDetail.DiscountType + " for Student Code : " + DisDetail.StudentID;
            DisAttach.Show();
        }

        
    }
}